<template>
  <view>
    <view class="wrap">


      <u-swiper :title="false" @click="swiperClick" mode="none" :list="listArray" :effect3d="true"
                effect3d-previous-margin="150" :height="swiperHeight2" :autoplay="false" img-mode="scaleToFill">
      </u-swiper>


    </view>

    <downloadMaterial :type="3" ref="downloadMaterial" :shopId="shopId" @changeBelongMemberId="changeBelongMemberId"></downloadMaterial>
    <u-modal v-model="showIsGoThirdAccountPage" :show-cancel-button="true" confirm-text="前往"
             @confirm="goToThirdAccountPage" content="暂未授权三方账号，请前往授权"></u-modal>

    <toUserAuth ref="toUserAuth"
                :invitMsg="invitMsg"
                :isDisabled="isDisabled"
                :invitationCode="invitationCode"
                :currentDoAuth="currentDoAuth"
                @indexGgRefresh="indexGgRefresh"></toUserAuth>
  </view>
</template>

<script>
import $ from "@/utils/api";
import toUserAuth from "@/pages/user/components/toUserAuth.vue";

import downloadMaterial from "@/pagesA/components/downloadMaterial/downloadMaterial.vue";

const APIDouyin = require('@/utils/api/douyin.js').default;
const API = require('@/utils/api/find.js').default;
export default {
  components: {downloadMaterial,toUserAuth},
  name: "findSwiper",

  props: {
    recommend:{
      required: true,
      default: false,
    },
    trailerLink: {
      required: String,
      default: "",
    },
    shopId: {
      required: Number
    },
    type: {
      required: Number,
      default: 0,
    },
    listArray: {
      required: Array,
      default: [],
    },
    listVideoOrImg: {
      required: Array,
      default: [],
    },
    goodsId: {
      default: '',
    },
    userId: {
      default: ''
    },
    isOnlyOne: {
      type: Boolean,
      default: false
    },
    videoNum: {
      type: Number,
      default: 0
    },
    imgNum: {
      type: Number,
      default: 0
    },
  },
  created() {
    this.checkHasThirdAccount() //刚进入页面加载数据-查看会员是否有第三方账号
    this.swiperHeight = this.$u.sys().windowHeight - 98 - 255 - 20.8 - 45 - 50 - 50 -20-5-44
    this.swiperHeight2 = this.pxToRpx(this.swiperHeight+30)
  },
  data() {
    return {
      invitMsg: "商家邀请码：",
      isDisabled: false,
      invitationCode: "",
      currentDoAuth: 'add',
      showIsGoThirdAccountPage: false,
      swiperHeight2: 0,
      swiperHeight: 0,
      isShowWatermark: uni.getStorageSync('config').is_open_watermark_img == '1' ? true : false,
      watermarkContent: uni.getStorageSync('config').watermark_content_img,
      currentIndex: 0,
      currentItem: {}
    }
  },
  methods: {
    indexGgRefresh() {
      this.isLogin = uni.getStorageSync('token') ? true : false;
      this.isAuth = uni.getStorageSync('userInfo').shopStatus == 1 ? true : false;
    },
    checkHasThirdAccount() {
      const self = this;
      $.ajax({
        url: APIDouyin.selectThirdAccountByMemberId,
        data: {},
        method: 'GET',
        success(res) {
          self.thirdAccountList = res.data
        }
      })
    },
    goToThirdAccountPage() {
      this.go('/pagesA/thirdAccount/index');
    },
    pxToRpx(px) {
      //获取整个屏幕的宽度单位 px
      let screenWidth = wx.getSystemInfoSync().screenWidth
      //用整个屏幕的px单位 除以 750
      let ratio = 750 / screenWidth
      //用px单位除以 屏幕比
      return (px * ratio)
    },

    toShowDownload(trendsId) {
      this.$refs.downloadMaterial.toShowDownloadModal(trendsId)
    },
    changeBelongMemberId() {
      // this.dataList[0].belongMemberId=1
    },

    verifyTrendsDownloadByOther(indexClick) {
      const self = this
      self.listVideoOrImg.forEach((item, index) => {
        if (indexClick === index) {
          self.currentItem = item
        }
      })
      self.currentIndex = indexClick
      $.ajax({
        url: API.verifyTrendsDownloadIsOther,
        data: {
          "trendsId": self.currentItem.id
        },
        loading: true,
        method: 'GET',
        success(res) {
          self.preview(self.currentItem.id)
        },

      })
    },
    swiperClick(indexClick) {
      this.verifyTrendsDownloadByOther(indexClick)
    },
    initToUserAuth(authFromInvitCode, shopName) {
      this.$refs.toUserAuth.init(null, null, null, null, null, authFromInvitCode, shopName)
    },
    preview(trendsId) {
      let data = this.currentItem
      let isSelf = this.isSelf ? 1 : 0;
      if (data.type == 1) {

        let isOnlyOne = this.isOnlyOne
        let onlyOneTrendsId = "";
        if (isOnlyOne) {
          onlyOneTrendsId = data.id;
        }

        $.go('/pagesA/find/video?id=' + data.id + '&isSelf=' + isSelf + '&userId=' + this.userId + '&isGg=' + false + '&onlyOneTrendsId=' + onlyOneTrendsId+"&trailerLink="+this.trailerLink+"&goodsId="+this.goodsId+ '&shopId=' + this.shopId);
      } else if (data.type == 3) {

        let isLogin = uni.getStorageSync('token') ? true : false;
        let isAuth = uni.getStorageSync('userInfo').shopStatus == 1 ? true : false;
        if (!isLogin) {
          $.go('/pagesB/passport/login');
        } else {
          if (!isAuth && !this.recommend) {
            //商家素材但是没有绑定的情况
            $.$toast("请先认证关联商家");
            this.initToUserAuth("", "");
          } else if (this.thirdAccountList.length === 0) {
            //商品是平台推荐但是用户没有第三方账号
            this.showIsGoThirdAccountPage = true
          } else {
            this.toShowDownload(trendsId)
          }
        }





        let url = data.images
        if (this.isShowWatermark) {
          url = url + "-" + this.watermarkContent
        }
        uni.previewImage({
          urls: [url], // 需要预览的图片链接列表
        });
      } else {
        if (this.pages == 'user') {
          $.go('/pagesB/user/finddetail?id=' + data.id);
        } else {
          $.go('/pagesA/find/detail?id=' + data.id + '&isSelf=' + isSelf + '&userId=' + this.userId);
        }
      }
    },
  }
}
</script>


<style scoped>
.wrap {
  padding: 20rpx;
}
</style>



